$(function() {
	initTreeType();
    var settings = {
        url: ctx + "shopping/goods/list",
        pageSize: 10,
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                search_EQ_name: $(".goods-table-form").find("input[name='search_EQ_name']").val(),
                search_GTE_price: $(".goods-table-form").find("input[name='search_GTE_price']").val(),
                search_LTE_price: $(".goods-table-form").find("input[name='search_LTE_price']").val(),
                search_LIKE_typeIds: $(".goods-table-form").find("input[name='search_LIKE_typeIds']").val()
            };
        },
        columns: [{
                checkbox: true
            } ,{
                field: 'id',
                visible: false
            }, {
                field: 'name',
                title: '名称'
            }, {
                field: 'price',
                title: '价格'
            }, {
                field: 'pic',
                title: '图片',
                formatter: function(value, row, index) {
                	var context = "";
            		if(value!=null && value!=""){
            			var imgArr = value.split(";");
            			$(imgArr).each(function(i,item){
            				if(item!=""){
            					context+="<img src='"+item+"' width='25' height='25' style='margin-left: 10px;'>"
            				}
            			})
            		}
            		return context;
                }
            }, {
                field: 'info',
                title: '说明'
            }]
    }
    $MB.initTable('goodsTable', settings);
});


function search() {
    $MB.refreshTable('goodsTable');
}

function refresh() {
    $(".goods-table-form")[0].reset();
    $MB.refreshTable('goodsTable');
}

function deletes() {
    var selected = $("#goodsTable").bootstrapTable('getSelections');
    var selected_length = selected.length;
    var contain = false;
    if (!selected_length) {
        $MB.n_warning('请勾选需要删除的商品！');
        return;
    }
    
    var ids = "";
    for (var i = 0; i < selected_length; i++) {
        ids += selected[i].id;
        if (i != (selected_length - 1)) ids += ",";
    }

    $MB.confirm({
        text: "确定删除选中商品？",
        confirmButtonText: "确定删除"
    }, function() {
        $.post(ctx + 'shopping/goods/delete', { "ids": ids }, function(r) {
            if (r.code == 0) {
                $MB.n_success(r.msg);
                refresh();
            } else {
                $MB.n_danger(r.msg);
            }
        });
    });
}

function exportExcel(){
	$.post(ctx+"shopping/goods/excel",$(".goods-table-form").serialize(),function(r){
		if (r.code == 0) {
			window.location.href = "common/download?fileName=" + r.msg + "&delete=" + true;
		} else {
			$MB.n_warning(r.msg);
		}
	});
}

function exportCsv(){
	$.post(ctx+"shopping/goods/csv",$(".goods-table-form").serialize(),function(r){
		if (r.code == 0) {
			window.location.href = "common/download?fileName=" + r.msg + "&delete=" + true;
		} else {
			$MB.n_warning(r.msg);
		}
	});
}


function initTreeType(){
	$.post(ctx + "shopping/type/typeNameTree", {}, function(r) {
		if (r.code == 0) {
			var data = r.msg;
			data.text = '全部类型';
			
			$('#treeType').jstree({
                "core": {
                    'data': data,
                    'themes':{
                    	"theme" : "default",
                    	'icons':false,
                    	"dots" : false
                    }
                },
                "plugins" : [ "changed","wholerow","search" ]
            }).on("changed.jstree", function (e, data) {
            	var selectedNode = data.changed.selected[0];
            	if(selectedNode!=undefined){
            		$("input[name='search_LIKE_typeIds']").val(selectedNode == 0? "" : "-"+selectedNode+"-");
            		search();
            	}
            });
			
			//属性菜单搜索 - 工序文本搜索
			$(".list-tree-left .search_bar .input_search").keyup(function () {
				var v = $(this).val();
                $('#treemenu').jstree(true).search(v);
            });
		}
    })
}